<?php

declare(strict_types=1);

namespace app\index\controller;

use app\admin\model\IndexRule;
use app\common\controller\IndexController;
use think\facade\Cache;
use think\facade\View;
use think\Request;

class Index extends IndexController
{

    public function initialize()
    {
        parent::initialize();
        $this->clearCache();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $pac_url = $this->request->domain() . '/pac/' . $this->modelUser->uid . '.pac';
        $pac_preview_url = $this->request->domain() . '/pac/' . $this->modelUser->uid . '.pac?preview=1';

        $list_rule = IndexRule::where('user_id', $this->modelUser->id)->order('content asc')->select();

        $list_status = IndexRule::$statusList;

        View::assign('list_status', $list_status);
        View::assign('list_rule', $list_rule);

        View::assign('pac_url', $pac_url);
        View::assign('pac_preview_url', $pac_preview_url);

        return View::fetch();
    }

    public function saveRule()
    {
        $post_content = $this->request->post('content');

        $post_content = trim($post_content);


        if (empty($post_content)) {
            return json_message('提交数据不能为空');
        }

        $post_list = explode("\n", $post_content);

        foreach ($post_list as $rule) {
            $model_rule = IndexRule::where('content', $rule)->where('user_id', $this->modelUser->id)->find();

            if (empty($model_rule)) {
                $model_rule = new IndexRule();

                $model_rule->user_id = $this->modelUser->id;
                $model_rule->content = $rule;
                $model_rule->save();
            }
        }

        return json_message();
    }

    public function updateRule($id)
    {
        $model_rule = IndexRule::find($id);
        $post_data = $this->request->post();

        $content = $model_rule->content;

        if (isset($post_data['status'])) {

            if (strpos($content, '@@') === false) {

                if ($post_data['status'] == 'public') {
                    return json_message(['status' => $model_rule->status], 500, '这条规则不允许设置为公开库');
                }
            }
        }

        $model_rule->save($post_data);

        return json_message();
    }

    public function updatePacMode($pac_mode)
    {
        $this->modelUser->pac_mode = $pac_mode;

        $this->modelUser->save();

        return json_message();
    }

    public function updatePacProxy($pac_proxy)
    {
        $this->modelUser->pac_proxy = $pac_proxy;

        $this->modelUser->save();

        return json_message();
    }


    public function deleteRule($id)
    {
        $model_rule = IndexRule::find($id);

        if ($model_rule->user_id != $this->modelUser->id) {
            return json_message('请求错误');
        }

        $model_rule->delete();

        return json_message();
    }

    public function clearCache()
    {
        $pac_content_key = 'pac_content_' . $this->modelUser->uid;

        Cache::delete($pac_content_key);

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
